package linc.spring.demo;

import linc.spring.demo.config.ApplicationConfiguration;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

/**
 * @author yqlin
 * @date 2021/9/7 00:43
 * @description
 */
public class SpringApplicationContextTest {
    /**
     * 国际化
     */
    @Test
    public void testApplicationContext() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
        String message = applicationContext.getMessage("username", null, new Locale("en_cn"));
        System.out.println(message);
    }

    /**
     * 资源加载
     */
    @Test
    public void testResource() throws IOException {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
        Resource resource1 = applicationContext.getResource("http://let.linq.cool:1025/");
        System.out.println(resource1.contentLength());
        System.out.println(resource1.getURL());

        Resource resource2 = applicationContext.getResource("classpath:spring.xml");
        System.out.println(resource2.contentLength());
        System.out.println(resource2.getURL());
    }

    /**
     * 获取运行时环境
     * @PropertySource("classpath:spring.properties")
     * 来使得某个properties文件中的参数添加到运行时环境中
     */
    @Test
    public void testRuntimeEnvironment() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
        Map<String, Object> systemEnvironment = applicationContext.getEnvironment().getSystemEnvironment();
        systemEnvironment.forEach((key, value) -> System.out.printf("%s:%s%n", key, value));
        System.out.println("-----------------");
        MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
        System.out.println(propertySources);
        System.out.println("-----------------");
        Map<String, Object> systemProperties = applicationContext.getEnvironment().getSystemProperties();
        systemProperties.forEach((key, value) -> System.out.printf("%s:%s%n", key, value));
    }

}
